package com.imooc.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @program: hadoop
 * @description: HdfsOperator
 * @author: zengchen
 * @create: 2021-03-24 10:33
 **/
public class HdfsOperator {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://centos7:9000");
        FileSystem fileSystem = FileSystem.get(configuration);
//        FileSystem fileSystem = FileSystem.get(new URI("hdfs://centos7:9000"), configuration, "root");

         mkdir(fileSystem);

//        Path path = new Path("/java");
//        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(path, true);
//        while (files.hasNext()){
//            LocatedFileStatus next = files.next();
//            BlockLocation[] blockLocations = next.getBlockLocations();
//            for (BlockLocation blockLocation : blockLocations) {
//                System.out.println(blockLocation.getOffset() + "," + blockLocation.getLength()
//                        + "," + Arrays.toString(blockLocation.getHosts())+ "," + Arrays.toString(blockLocation.getCachedHosts()));
//            }
//        }

    }

    private static void mkdir(FileSystem fileSystem) throws IOException, InterruptedException, URISyntaxException {

        Path path = new Path("/java1");
        boolean mkdirs = fileSystem.mkdirs(path);
        System.out.println("mkdir:" + mkdirs);
    }
}
